package Reflect.MyReflect002;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * ClassName:MyReflect
 * Package:Reflect.MyReflect002
 * Description:
 *
 * @Author:ZhangXin
 * @Create:2023/3/8-20:12
 * @Version:v1.0
 */
public class MyReflect {
    /*Class类中用于获取构造方法的方法
    * Constructor<?>[]getConstructors()
    * Constructor<?>[]getDeclaredConstructor()
    * Constructor<T>getConstructor(Class<?>...parameterTypes)
    * Constructor<T>getDeclaredConstructor(Class<?>...parameterTypes)
    *
    * Constructor类中用于创建对象的方法
    *   T newInstance(Object...initargs)
    *   setAccessible(boolean flag)*/
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //1.获取Class字节码文件对象
        Class clazz = Class.forName("Reflect.MyReflect002.Student");

        //2.获取构造方法
//        Constructor[] cons1 = clazz.getConstructors();
//        for (Constructor con : cons1) {
//            System.out.println(con);
//        }

//        Constructor[] cons2 = clazz.getDeclaredConstructors();
//        for (Constructor con : cons2) {
//            System.out.println(con);
//        }

//        Constructor cons3 = clazz.getDeclaredConstructor();
//        System.out.println(cons3);

//        Constructor cons4 = clazz.getDeclaredConstructor(String.class);
//        System.out.println(cons4);

//        Constructor cons5 = clazz.getDeclaredConstructor(int.class);
//        System.out.println(cons5);

        Constructor cons6 = clazz.getDeclaredConstructor(String.class, int.class);
//        System.out.println(cons6);

//        int modifiers = cons6.getModifiers();
//        System.out.println(modifiers);
//
//
//        Student t1 = new Student();
//        Parameter[] parameters = cons6.getParameters();
//        for (Parameter parameter : parameters) {
//            System.out.println(parameter);
//        }

        //暴力反射：临时取消权限校验
        cons6.setAccessible(true);
        Student stu = (Student) cons6.newInstance("zhangsan", 23);
        System.out.println(stu);



    }
}
